<template>
  <el-table :data="tableData" stripe border style="width: 100%" row-key="id" :expand-row-keys="expandedRowKeys"
            @expand-change="handleExpandChange">
    <el-table-column type="expand" label="详细数据" width="90">
      <template #default="props">
        <div>
          <el-table :data="props.row.detil" border>
            <el-table-column label="编号" prop="mdId" width="100"/>
            <el-table-column label="部门" prop="mdContact" width="200"/>
            <el-table-column label="标题" prop="resTitle"/>
            <el-table-column label="描述" prop="describtion"/>
            <el-table-column label="字段" prop="detailFields"/>
          </el-table>
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="complianceCheck" label="合规性检查"/>
    <el-table-column prop="dataCleaning" label="数据清洗"/>
    <el-table-column prop="formatConversion" label="格式转换"/>
    <el-table-column prop="intersection" label="交集"/>
    <el-table-column prop="sumAggregate" label="并集"/>
    <el-table-column prop="cartesianProduct" label="笛卡尔积"/>
    <el-table-column prop="statisticAnalysis" label="统计分析"/>
    <el-table-column prop="algorithmCalculation" label="算法计算"/>
    <el-table-column prop="dataMining" label="数据挖掘"/>
    <el-table-column prop="bigDataAnalysis" label="大数据分析"/>
    <el-table-column prop="privacy" label="隐私保护"/>
    <el-table-column prop="machineLearning" label="机器学习"/>
    <el-table-column label="是否通过" width="150">
      <template #default="scope">
        <el-button size="small" @click="passThrough(scope.row)">通过</el-button>
        <el-button size="small" type="danger" @click="noPass(scope.row)">不通过</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup lang="ts">
import {ref, onMounted} from 'vue'
import axios from "axios";
import {ElMessageBox, ElMessage} from "element-plus";
import data_product_requirements_definition from "@/stores/data_product_requirements_definition";

onMounted(() => {
  load()
})
const tableData = ref([])
const expandedRowKeys = ref([]);
const store = data_product_requirements_definition()
let data_processing_check = store.getDataProcessingCheck
let data_application_check = store.getDataApplicationCheck
const load = () => {
  axios.get('http://localhost:8081/dataCheckInfo').then(
      res => {
        if (res) {
          tableData.value = res.data.data
          tableData.value.forEach(item =>{
            if (item.detailFields!== null) item.detailFields = JSON.parse(item.detailFields)
          })
          tableData.value.forEach(item =>{
            if (item.task!== null){
              const numberArray: number[] = item.task.split(",").filter(str => str.trim() !== "").map(str => parseInt(str.trim()));
              numberArray.forEach((num, index) =>{
                num == 1 ?  item.complianceCheck = '√' :  item.complianceCheck = '×'
                num == 2 ?  item.dataCleaning = '√' :  item.dataCleaning = '×'
                num == 3 ?  item.formatConversion = '√' :  item.formatConversion = '×'
                num == 4 ?  item.intersection = '√' :  item.intersection = '×';
                num == 5 ?  item.sumAggregate = '√' :  item.sumAggregate = '×';
                num == 6 ?  item.cartesianProduct = '√' :  item.cartesianProduct = '×';
                num == 7 ?  item.statisticAnalysis = '√' :  item.statisticAnalysis = '×';
                num == 8 ?  item.algorithmCalculation = '√' :  item.algorithmCalculation = '×';
                num == 9 ?  item.dataMining = '√' :  item.dataMining = '×';
                num == 10 ?  item.bigDataAnalysis = '√' :  item.bigDataAnalysis = '×';
                num == 11 ?  item.privacy = '√' :  item.privacy = '×';
                num == 12 ?  item.machineLearning = '√' :  item.machineLearning = '×';
              })
            }
          })
        }
      }
  ).catch(e => {
    console.log(e)
  })
}

const handleExpandChange = (row: any) => {
    axios.get('http://localhost:8081/infoResMdInfo/listId', {
      params: {
        mdId1: row.mdId1,
        mdId2: row.mdId2,
        mdId3: row.mdId3
      }
    }).then(res => {
      if (res){
        row.detil = res.data.data
        row.detil.forEach(item =>{
          row.detailFields.forEach(item2 =>{
            // console.log(item2)
            if (item2[0] ===  item.mdId){
              let colomns = ''
              item2[1].forEach(item3 =>{
                colomns += item3.label + ','
              })
              item.detailFields =colomns
            }
          })
        })
      }
    })
};
const passThrough = (row: any) => {
  //axios.delete('')

  //数据库删除这个字段
  //发送到数据工厂处理
}
const noPass = (row: any) => {
//输入理由
  ElMessageBox.prompt('请输入不通过理由', '确定不通过吗', {
    confirmButtonText: '确定不给予通过',
    cancelButtonText: '取消',
    inputErrorMessage: '请输入不通过理由',
    draggable: true,
  }).then(() => {
    //给后台发送请求
    axios.delete('http://localhost:8081/dataCheckInfo/' + row.id).then(res => {
      if (res) {
        ElMessage.success('不通过成功')
      }
    }).catch(e => {
      ElMessage.error('请求失败')
    })
  })
}
</script>

<style scoped>

</style>